//
//  addJLGJViewController.m
//  Estate
//
//  Created by heyong on 16/6/21.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "ProcessRecordModel.h"
#import "SegmentViewController.h"
#import "B_addJLGJViewController.h"

#import "addJGDBViewController.h"

@interface addJGDBViewController ()

@end


@implementation addJGDBViewController

// 基本信息控制器
- (void)setAaddVC:(A_addJLGJViewController *)AaddVC {
    if (_AaddVC != AaddVC) {
        _AaddVC = AaddVC;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.isGJ) {
        self.title=@"跟进单";
    } else if (self.isDB) {
        self.title=@"督办单";
    }
    //添加右上方提交按钮
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];

    [self createsliderBtn];
}
- (void)clickSave {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addJGDB_ClickSave" object:nil];
}

- (void)createsliderBtn {
    
    SegmentViewController *vc = [[SegmentViewController alloc]init];
    vc.height = DEF_SCREEN_HEIGHT - 108;

    // 基本资料
    A_addJLGJViewController *a_gjCtr;
    if (!self.AaddVC) {
        a_gjCtr = [[A_addJLGJViewController alloc]init];
     } else {
        a_gjCtr = self.AaddVC;
    }
    a_gjCtr.addDuB = self.isDB;
    a_gjCtr.addGJ = self.isGJ;
    //巡检督办传入数据
    if(self.processTitle||self.processContent){
        a_gjCtr.processTitle=self.processTitle;
        a_gjCtr.processContent=self.processContent;
    }
    
    // 附件信息
    B_addJLGJViewController *b_gjCtr=[[B_addJLGJViewController alloc]init];
    b_gjCtr.addGJ = self.isGJ;
    b_gjCtr.addDuB = self.isDB;
    if(self.inspect){
        //巡检发起督办 带入整改前附件不允许删除
        b_gjCtr.isInspect=YES;
        b_gjCtr.inspectFileArray=[NSMutableArray arrayWithArray:self.inspectFileArr];
    } else {
        //巡检督办传入整改前附件
        if(self.inspectFileArr&&self.inspectFileArr.count>0){
            b_gjCtr.inspectFileArray=[NSMutableArray arrayWithArray:self.inspectFileArr];
            //巡检发起督办 带入整改前附件不允许删除
            b_gjCtr.isInspect=YES;
        }
    }
    //允许编辑
    if(self.edit){
        //基本信息传递编辑model
        ProcessRecordModel *model= [self loadProcessRecordData];
        a_gjCtr.recordModel=model;
        //基本信息可以编辑
        a_gjCtr.edit=YES;
        //基本信息流程id
        a_gjCtr.superProcessId=self.processId;
        //设置附件
        NSArray *imageArr=[LSUtility dictionaryWithJsonString:model.FILEINFOS];
        //查询附件表
        if(imageArr.count>0){
            NSMutableArray *fileUrl=[NSMutableArray array];
            for (NSDictionary *imageDic in imageArr) {
                NSString *fileId=imageDic[@"fileId"];
                NSString *fileName=[LSUtility queryAttachment:fileId][@"FILE_NAME"];
                NSString *filePath=[DEF_FILE_PATH stringByAppendingString:
                                    [NSString stringWithFormat:@"/%@",fileName]];
                [fileUrl addObject:filePath];
            }
            //附件信息id  设置附件信息
            b_gjCtr.processId=model.ID;
            b_gjCtr.selectedAssetURLs =[NSMutableArray arrayWithArray:fileUrl];
        }
    }
    
    // 添加基本信息, 附件信息
    NSMutableArray *controlArray = [[NSMutableArray alloc]initWithObjects:a_gjCtr, b_gjCtr, nil];
    //设置选中字体颜色
    vc.titleSelectedColor = [TTTheme defaultManger].defaultColor;
    // 添加标题
    vc.titleArray = @[@"基本资料", @"附件信息"];
    vc.buttonWidth = DEF_SCREEN_WIDTH / 2;
    //设置子视图
    vc.subViewControllers = controlArray;
    //标签栏背景色
    vc.headViewBackgroundColor=[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    //非选中状态下标签字体大小
    vc.fontSize=14.;
    //选中下划线颜色
    vc.bottomLineColor=[TTTheme defaultManger].defaultColor;
    vc.buttonHeight = 40.;
    
    [vc initSegment];
    [vc addParentController:self];
}

#pragma mark-编辑查询流程记录表
- (ProcessRecordModel *)loadProcessRecordData {
    NSString *sqlStr=[NSString stringWithFormat:@"SELECT * FROM PROCESS_RECORD where PROCESS_ID='%@' ORDER BY CREATE_TIME",self.processId];
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    FMResultSet *resule=[db executeQuery:sqlStr];
    NSMutableArray *array=[NSMutableArray array];
    while ([resule next]) {
        NSDictionary *dic=[resule resultDictionary];
        
        ProcessRecordModel *model=[ProcessRecordModel yy_modelWithDictionary:dic];
        [array addObject:model];
    }
    return  [array lastObject];
}


@end

